package com.manage.project.biz.controller;


import com.manage.common.utils.poi.ExcelUtil;
import com.manage.framework.aspectj.lang.annotation.Log;
import com.manage.framework.aspectj.lang.enums.BusinessType;
import com.manage.framework.web.controller.BaseController;
import com.manage.framework.web.domain.AjaxResult;
import com.manage.framework.web.page.TableDataInfo;
import com.manage.project.biz.domain.BizJidiansheshiguzhangxuncha;
import com.manage.project.biz.domain.BizShebei;
import com.manage.project.biz.domain.Bizjidianpingdingfenxiang;
import com.manage.project.biz.service.IBizJidiansheshiguzhangxunchaService;
import com.manage.project.biz.service.IBizShebeiService;
import com.manage.project.biz.service.IBizjidianpingdingfenxiangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 【】Controller
 *
 * @author ruoyi
 * @date 2022-05-10
 */
@RestController
@RequestMapping("/biz/jidianpingdingfenxiang")
public class BizjidianpingdingfenxiangController extends BaseController
{
    @Autowired
    private IBizjidianpingdingfenxiangService bizjidianpingdingfenxiangService;

    @Autowired
    private IBizJidiansheshiguzhangxunchaService bizJidiansheshiguzhangxunchaService;

    @Autowired
    private IBizShebeiService bizShebeiService;


    /**
     * 查询【】列表
     */
//    @PreAuthorize("@ss.hasPermi('biz:jidianpingdingfenxiang:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bizjidianpingdingfenxiang bizjidianpingdingfenxiang)
    {
        startPage();
        List<Bizjidianpingdingfenxiang> list = bizjidianpingdingfenxiangService.selectBizjidianpingdingfenxiangList(bizjidianpingdingfenxiang);
        return getDataTable(list);
    }

    /**
     * 导出【】列表
     */
    @Log(title = "技术状况评定分项", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Bizjidianpingdingfenxiang bizjidianpingdingfenxiang)
    {
        List<Bizjidianpingdingfenxiang> list = bizjidianpingdingfenxiangService.selectBizjidianpingdingfenxiangList(bizjidianpingdingfenxiang);
        ExcelUtil<Bizjidianpingdingfenxiang> util = new ExcelUtil<Bizjidianpingdingfenxiang>(Bizjidianpingdingfenxiang.class);
        return util.exportExcel(list, "技术状况评定分项记录");
    }
//    @PreAuthorize("@ss.hasPermi('biz:jidianpingdingfenxiang:export')")
//    @Log(title = "【评定分项】", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Bizjidianpingdingfenxiang bizjidianpingdingfenxiang) throws IOException {
//        List<Bizjidianpingdingfenxiang> list = bizjidianpingdingfenxiangService.selectBizjidianpingdingfenxiangList(bizjidianpingdingfenxiang);
//        ExcelUtil<Bizjidianpingdingfenxiang> util = new ExcelUtil<Bizjidianpingdingfenxiang>(Bizjidianpingdingfenxiang.class);
//        util.exportExcel(response, list, "【请填写功能名称】数据");
//    }

    /**
     * 获取【】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('biz:jidianpingdingfenxiang:query')")
    @GetMapping(value = "/{subentryId}")
    public AjaxResult getInfo(@PathVariable("subentryId") Long subentryId)
    {
        return AjaxResult.success(bizjidianpingdingfenxiangService.selectBizjidianpingdingfenxiangById(subentryId));
    }

    /**
     * 新增【】
     */
//    @PreAuthorize("@ss.hasPermi('biz:jidianpingdingfenxiang:add')")
    @Log(title = "【评定分项】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bizjidianpingdingfenxiang bizjidianpingdingfenxiang) throws ParseException {
        // 获取当前表所有数据
        Bizjidianpingdingfenxiang bizjidianpingdingfenxiang1 = new Bizjidianpingdingfenxiang();
        List<Bizjidianpingdingfenxiang> listPDFX = bizjidianpingdingfenxiangService.selectBizjidianpingdingfenxiangList(bizjidianpingdingfenxiang1);
        BizShebei bizShebei = new BizShebei();
        List<BizShebei> listShebei = bizShebeiService.selectBizShebeiList(bizShebei);
        int shebeiID = bizjidianpingdingfenxiang.getElectricalEquipmentId();// 5位数
        // 验证设备类型编码 和 隧道名称 对应关系
        String suidaoName = bizjidianpingdingfenxiang.getTunnelName();
        int flag1 = 0;// 隧道名称 与 设备类型编码 的关系
        for (int i=listShebei.size()-1; i>=0; i--){
            if ((suidaoName.equals(listShebei.get(i).getField10())) && (shebeiID == Integer.parseInt(listShebei.get(i).getField1().substring(0, 5)))){
                flag1=1;
                break;
            }
        }
        if (flag1 == 0){
            return AjaxResult.error("该隧道内无该类设备");
        }
        // 根据设备类型编码获取故障台数 和 故障天数
        BizJidiansheshiguzhangxuncha bizgzxc = new BizJidiansheshiguzhangxuncha();// 获取所有故障记录
        List<BizJidiansheshiguzhangxuncha> listBizgzxc = bizJidiansheshiguzhangxunchaService.selectBizJidiansheshiguzhangxunchaList(bizgzxc);
        int gznum = 0;// 故障台数
        for (int i=0; i<listBizgzxc.size(); i++){
            if (shebeiID == Integer.parseInt(listBizgzxc.get(i).getField15().substring(0, 5))){
                gznum++;
            }
        }
        SimpleDateFormat fo = new SimpleDateFormat("yyyy-MM-dd");
        Date today = new Date();// 当前日期
        Date gzDate = new Date();// 故障日期
        long avgdays1 = 0; //（平均）故障天数
        for (int i=0; i<listBizgzxc.size(); i++){
            if (shebeiID == Integer.parseInt(listBizgzxc.get(i).getField15().substring(0, 5))){
                gzDate = fo.parse(listBizgzxc.get(i).getField5());
                long diff1 = today.getTime() - gzDate.getTime();
                long gzdays = (diff1) / 1000 / 60 / 60 / 24;
                System.out.println(gzdays);
                avgdays1+=gzdays;
            }
        }
        // （平均）故障天数 = 当前日期 - 故障日期
        avgdays1/=gznum;
        System.out.println(avgdays1);

        // 根据设备类型编码获取总台数 和 日历天数（平均工作天数）
        int totalNum = 0;// 总台数
        for (int i=0; i<listShebei.size(); i++){
            if (shebeiID == Integer.parseInt(listShebei.get(i).getField1().substring(0, 5))){
                totalNum++;
            }
        }
        Date workDate = new Date();// 工作日期
        long avgdays2 = 0; // （平均）工作天数
        for (int i=0; i<listShebei.size(); i++){
            if (shebeiID == Integer.parseInt(listShebei.get(i).getField1().substring(0, 5))){
                workDate = fo.parse(listShebei.get(i).getField8());
                long diff2 = today.getTime() - workDate.getTime();
                long workdays = (diff2) / 1000 / 60 / 60 / 24;
                System.out.println(workdays);
                avgdays2+=workdays;
            }
        }
        // （平均）工作天数 = 当前日期 - 安装日期
        avgdays2/=totalNum;
        System.out.println(avgdays2);
        // 设备完好率
        int whl = Math.round((1 - (gznum * avgdays1)/(totalNum * avgdays2)) * 100);
        bizjidianpingdingfenxiang.setEquipmentCompleteness(whl);
        // 根据设备类型判断权重
        int shebeiTypeNum = bizjidianpingdingfenxiang.getElectricalEquipmentId() / 10000;
        if (shebeiTypeNum == 1){
            bizjidianpingdingfenxiang.setWeight(23);
        } else if (shebeiTypeNum == 2) {
            bizjidianpingdingfenxiang.setWeight(18);
        }else if (shebeiTypeNum == 3) {
            bizjidianpingdingfenxiang.setWeight(19);
        }else if (shebeiTypeNum == 4) {
            bizjidianpingdingfenxiang.setWeight(21);
        }else if (shebeiTypeNum == 5) {
            bizjidianpingdingfenxiang.setWeight(19);
        }
        // 获取上次评定日期
        int flag = 0; // 有无上次评定记录
        for (int i=0; i<listPDFX.size(); i++){
            if (shebeiID == listPDFX.get(i).getElectricalEquipmentId()){
                flag = 1;
                bizjidianpingdingfenxiang.setLastRateTime(listPDFX.get(i).getRateTime());break;
            }
        }
        if (flag == 0){
            bizjidianpingdingfenxiang.setLastRateTime(null);
        }
        // 本次评定日期
        bizjidianpingdingfenxiang.setRateTime(today);
        return toAjax(bizjidianpingdingfenxiangService.insertBizjidianpingdingfenxiang(bizjidianpingdingfenxiang));
    }

    /**
     * 修改【】
     */
//    @PreAuthorize("@ss.hasPermi('biz:jidianpingdingfenxiang:edit')")
    @Log(title = "【评定分项】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bizjidianpingdingfenxiang bizjidianpingdingfenxiang)
    {
        return toAjax(bizjidianpingdingfenxiangService.updateBizjidianpingdingfenxiang(bizjidianpingdingfenxiang));
    }

    /**
     * 删除【】
     */
//    @PreAuthorize("@ss.hasPermi('biz:jidianpingdingfenxiang:remove')")
    @Log(title = "【评定分项】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{subentryIds}")
    public AjaxResult remove(@PathVariable Long[] subentryIds)
    {
        return toAjax(bizjidianpingdingfenxiangService.deleteBizjidianpingdingfenxiangByIds(subentryIds));
    }
}
